package middleware

import (
	"app/wd"
	"runtime/debug"

	"github.com/gin-gonic/gin"
)

// CustomRecovery 自定义恢复
func customRecovery(c *gin.Context, err interface{}) {
	wd.Failed(c, wd.NewResponseError(wd.StatusCode500, "系统出问题了，请稍后再试", err))
}

// SetCustomRecovery 设置自定义的Recovery函数
func SetCustomRecovery() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				// 记录错误信息
				stackInfo := string(debug.Stack())
				wd.App.Logger.Errorf("panic recover :%+v\nstack:%s", err, stackInfo)
				customRecovery(c, err)
			}
		}()
		c.Next()
	}
}
